<template>
    <div class="mainDiv">
        <div class="rowHead">
            <el-button icon="plus" type="success" @click="add" v-qx="permission.add">新增厂商</el-button>
            <el-select v-model="page.zt" class="ml" @change="selstatus" v-qx="permission.status">
                <el-option v-for="(s, k) in status" :label="s.txt" :value="s.val" :key="s.val">
                </el-option>
            </el-select>
            <el-button icon="h-shuaxin1" type="primary" class="buttonLeft" @click="searchList">刷新列表</el-button>
            <el-input class="searchArea" v-model="page.search" placeholder="搜索厂商名称、厂商描述" @keyup.enter.native="searchList">
                <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="searchList"></i>
            </el-input>
        </div>
        <el-table :data="list" :stripe="true" height="100%">
            <el-table-column prop="id" label="厂商ID"></el-table-column>
            <el-table-column prop="name" label="厂商名称"></el-table-column>
            <el-table-column prop="remarks" label="厂商描述" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="isdisabled" label="厂商状态">
                <template slot-scope="scope">
                    <Tags :typeValue="scope.row.isdisabled" typeName="isDisable"></Tags>
                </template>
            </el-table-column>
            <el-table-column prop="addtime" label="添加时间">
                <template slot-scope="scope">
                    {{scope.row.addtime|hourTime}}
                </template>
            </el-table-column>
            <el-table-column label="设置">
                <template slot-scope="scope">
                    <el-tooltip content="关闭厂商" placement="left" :open-delay="800" v-if="!scope.row.isdisabled" v-qx="permission.unstop">
                        <el-button icon="el-icon-h-jinyong" type="danger" size="mini" @click="stop(scope.row.id,1)"></el-button>
                    </el-tooltip>
                    <el-button-group v-else>
                        <el-tooltip content="厂商编辑" placement="left" :open-delay="800" v-qx="permission.edit">
                            <el-button icon="el-icon-h-edit" type="warning" size="mini" @click="edit(scope.row)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="重新打开厂商" placement="top" :open-delay="800" v-qx="permission.unstop">
                            <el-button icon="el-icon-h-qiyong2" type="success" size="mini" @click="stop(scope.row.id,0)"></el-button>
                        </el-tooltip>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>
        <div class="block center bg-light">
            <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="page.pages" :page-sizes="[15, 30, 50, 100]" :page-size="page.limits" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import C from '#/src/common/common.js'
import Tags from '@/components/orderTags.vue'
import Factory from '#/src/organization/factory.js'

export default {
    name: 'factoryList',
    data () {
        return {
            list: [],
            total: 0,
            page: {
                pages: 1,
                limits: 15,
                search: '',
                zt: 0,
            },
            status: [{
                txt: '正常',
                val: 0
            }, {
                txt: '停用',
                val: 1
            }],
            permission: {
                add: 'factory_add',
                status: 'factory_status',
                edit: 'factory_edit',
                unstop: 'factory_unstop',
            }
        }
    },
    methods: {
        add: async function () {
            let res = await Factory.openAddFactory()
            if (res && res.data) {
                C.success(res.data)
                this.updateList()
            }
        },
        edit: async function (row) {
            let res = await Factory.openAddFactory(row)
            if (res && res.data) {
                C.success(res.data)
                this.updateList()
            }
        },
        stop: async function (id, st) {
            let that = this
            if (st) {
                C.confirm('关闭当前厂商, 是否继续?', async () => {
                    let res = await C.ajax('/organization/factory/unstop', { id: id, st: st }, 'post')
                    if (res) {
                        that.updateList()
                        C.success(res.data)
                    }
                })
            } else {
                let res = await C.ajax('/organization/factory/unstop', { id: id, st: st }, 'post')
                if (res) {
                    C.success(res.data)
                    that.updateList()
                }
            }
        },
        selstatus: function () {
            this.updateList()
        },
        searchList: function () {
            this.updateList()
        },
        sizeChange: function (val) {
            this.page.limits = val
            this.updateList()
        },
        currentChange: function (val) {
            this.page.pages = val
            this.updateList()
        },
        updateList: async function () {
            let data = await C.ajax('/organization/factory/factorylist', this.page, 'post')
            if (data) {
                this.total = data.count
                this.list = data.data
            }
        },
    },
    mounted: function () {
        this.updateList()
    },
    components: { Tags }
}
</script>